package com.genghis.ptas.inbox;

import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.inbox.entity.Inbox;
import com.genghis.ptas.inbox.service.InboxService;
import com.genghis.ptas.security.entity.User;
import com.genghis.ptas.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Controller
@Transactional
@RequestMapping("/inbox/")
public class InboxController {
    @Autowired
    private InboxService inboxService;

    @RequestMapping(value = "listAllInbox", method = {RequestMethod.POST})
    @ResponseBody
    public List<Inbox> listAllInbox(HttpServletRequest request, Inbox inbox) {
        return inboxService.listAllInbox(request, inbox);
    }

    @RequestMapping(value = "getPageMap", method = {RequestMethod.POST})
    @ResponseBody
    public Map getPageMap(HttpServletRequest request, Inbox inbox) {
        int countInbox = inboxService.listAllInbox(request, inbox).size();
        int page = null == request.getParameter("page") || "".equals(request.getParameter("page")) ? 1 : Integer.parseInt(request.getParameter("page"));
        int pageCount = 30;
        return Page.getPageMap(page, pageCount, countInbox);
    }

    @RequestMapping(value = "addInbox", method = {RequestMethod.POST})
    @ResponseBody
    public void addInbox(HttpServletRequest request, Inbox inbox) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        inbox.setUserId(user.getId());
        inbox.setUser(user);
        inboxService.addSentInbox(inbox);
    }

    @RequestMapping(value = "checkMail", method = {RequestMethod.POST})
    @ResponseBody
    public Inbox checkMail(HttpServletRequest request) {
        String id = request.getParameter("id");
        return inboxService.findInboxById(Integer.parseInt(id));
    }

    @RequestMapping(value = "listAllInboxDo", method = {RequestMethod.GET})
    public String listAllInboxDo(HttpServletRequest request, Inbox inbox) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        inbox.setUserId(user.getId());
        inbox.setState(DictConstant.MINUS_ONE);
        inbox.setCategory(DictConstant.INBOX_CATEGORY_1);
        request.setAttribute("countInbox", inboxService.countInboxCategory(inbox));
        inbox.setCategory(DictConstant.INBOX_CATEGORY_2);
        request.setAttribute("countSent", inboxService.countInboxCategory(inbox));
        inbox.setCategory(DictConstant.INBOX_CATEGORY_3);
        request.setAttribute("countDraft", inboxService.countInboxCategory(inbox));
        inbox.setCategory(DictConstant.VOID_VALUE);
        inbox.setState(DictConstant.INBOX_STATE_4);
        request.setAttribute("countTrash", inboxService.countInboxCategory(inbox));
        request.setAttribute("user", user);


        return "frontstage/person/myInbox";
    }

    @RequestMapping(value = "changeMassageState", method = {RequestMethod.POST})
    @ResponseBody
    public boolean changeMassageState(HttpServletRequest request) {
        try {
            String state = request.getParameter("state");
            String ids = request.getParameter("id");
            inboxService.changeMassageState(state, ids);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @RequestMapping(value = "changeMassageCategory", method = {RequestMethod.POST})
    @ResponseBody
    public boolean changeMassageCategory(HttpServletRequest request) {
        try {
            String category = request.getParameter("category");
            String ids = request.getParameter("id");
            inboxService.changeMassageCategory(category, ids);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @RequestMapping(value = "delMassage", method = {RequestMethod.POST})
    @ResponseBody
    public boolean delMassage(HttpServletRequest request) {
        try {
            String ids = request.getParameter("id");
            inboxService.delMassage(ids);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
